// modifiers
// methods: modify title according to url
// export: titleModifier

// remove substring from title
function modRemove(title, substr) {
    return title.replace(substr, "")
};

// split title by sep, trim n parts from left
function modTrimLeft(title, sep, n) {
    let titleSplit = title.split(sep);
    let titleMod = titleSplit.slice(n, titleSplit.length).join(sep);
    return titleMod
};

// split title by sep, trim n parts from right
function modTrimRight(title, sep, n) {
    let titleSplit = title.split(sep);
    let titleMod = titleSplit.slice(0, titleSplit.length-n).join(sep);
    return titleMod
};

// split title by sep, keep n parts from left
function modKeepLeft(title, sep, n) {
    let titleSplit = title.split(sep, n);
    let titleMod = titleSplit.join(sep);
    return titleMod
};

// split title by sep, keep n parts from right
function modKeepRight(title, sep, n) {
    let titleSplit = title.split(sep);
    let titleMod = titleSplit.slice(titleSplit.length-n, titleSplit.length).join(sep);
    return titleMod
};

// collection of modifiers
// order by alphabet
export let titleModifier = {
    acs: {
        target: "pubs.acs.org",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    annulreviews: {
        target: "annualreviews.org",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    aps: {
        target: "aps.org",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    arxiv: {
        target: "arxiv.org",
        modifier: (title) => modRemove(title, /^\[.*?\]/)
    },
    bilibili: {
        target: "bilibili.com",
        modifier: (title) => modTrimRight(title, "_", 2)
    },
    biomedcentral: {
        target: "biomedcentral.com",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    biorxiv: {
        target: "biorxiv.org",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    cell: {
        target: "cell.com",
        modifier: (title) => modTrimRight(title, ": ", 1)
    },
    csdn: {
        target: "csdn.net",
        modifier: function (title) {
            title = modRemove(title, "-CSDN博客"); // remove "-CSDN博客"
            title = modRemove(title, /^\(.*?\)/); // remove "(1条信息)"
            return title
        }
    },
    douban: {
        target: "douban.com",
        modifier: (title) => modRemove(title, /\(.*?\)$/)
    },
    elife: {
        target: "elifesciences.org",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    embo: {
        target: "embopress.org",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    frontiers: {
        target: "frontiersin.org",
        modifier: (title) => modTrimLeft(title, " | ", 1)
    },
    icur: {
        target: "iucr.org",
        modifier: (title) => modRemove(title, /^\(.*?\)/)
    },
    ieeexplore: {
        target: "ieeexplore.ieee.org",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    iop: {
        target: "iopscience.iop.org",
        modifier: (title) => modTrimRight(title, " - ", 1)
    },
    jneurosci: {
        target: "jneurosci.org",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    libgen: {
        target: RegExp("(libgen\\.[a-z]{2}|gen\\.lib\\.rus\\.ec)"),
        modifier: (title) => modTrimLeft(title, ":", 1)
    },
    mdpi: {
        target: "mdpi.com",
        modifier: (title) => modKeepRight(title, " | ", 1)
    },
    nature: {
        target: "nature.com",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    oup: {
        target: "academic.oup.com",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    plos: {
        target: RegExp("plos.*\\.org"),
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    pnas: {
        target: "pnas.org",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    portlandpress: {
        target: "portlandpress.com",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    scitationorg: {
        target: "scitation.org",
        modifier: (title) => modRemove(title, /:[^:]*?: Vol.*$/)
    },
    rcs: {
        target: "pubs.rsc.org",
        modifier: (title) => modTrimRight(title, " - ", 1)
    },
    royalsocietypub: {
        target: "royalsocietypublishing.org",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    sagepub: {
        target: "sagepub.com",
        modifier: (title) => modKeepLeft(title, " - ", 1)
    },
    semanticscholar: {
        target: "semanticscholar.org",
        modifier: (title) => modTrimLeft(modRemove(title, "| Semantic Scholar"), "]", 1)
    },
    science: {
        target: "science.org",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    sciencedirect: {
        target: RegExp("science-?direct\\.com"),
        modifier: (title) => modTrimRight(title, " - ", 1)
    },
    springerlink: {
        target: "link.springer.com",
        modifier: (title) => modKeepLeft(title, " | ", 1)
    },
    weread: {
        target: "weread.qq.com",
        modifier: (title) => modTrimRight(title, "-", 1)
    },
    wiley: {
        target: "onlinelibrary.wiley.com",
        modifier: (title) => modKeepLeft(modKeepLeft(title, " | ", 1), " - ", 1)
    },
    zhihu: {
        target: "zhihu.com",
        modifier: function (title) {
            title = modRemove(title, " - 知乎"); // remove "- 知乎"
            title = modRemove(title, /^\(.*?\)/); // remove "(1封私信 / 1条信息)"
            return title
        }
    }
};
